一键搭建k8s集群

2,792次阅读
没有评论
一键搭建 k8s 集群



输入下面脚本可一键搭建 k8s 集群

目前只适用于 centos 和 readhat,其它系统如果有需求可以留言。

bash <(curl -sL https://fastyw.com/wp-content/uploads/2022/12/k8s.sh)

脚本源代码

#!/bin/bash
# Author: 有人说我白

RED="\033[31m"      # Error message
GREEN="\033[32m"    # Success message
YELLOW="\033[33m"   # Warning message
BLUE="\033[36m"     # Info message
PLAIN='\033[0m'

colorEcho() {echo -e "${1}${@:2}${PLAIN}"
}

checkSystem() {result=$(id | awk '{print $1}')
    if [[ $result != "uid=0(root)" ]]; then
        colorEcho $RED "请以 root 身份执行该脚本"
        exit 1
    fi

    res=`which yum 2>/dev/null`
    if [[ "$?" != "0" ]]; then
        res=`which apt 2>/dev/null`
        if [[ "$?" != "0" ]]; then
            colorEcho $RED "不受支持的 Linux 系统"
            exit 1
        fi
        PMT="apt"
        CMD_INSTALL="apt install -y"
        CMD_REMOVE="apt remove -y"
        CMD_UPGRADE="apt update && apt upgrade -y; apt autoremove -y"
    else
        PMT="yum"
        CMD_INSTALL="yum install -y"
        CMD_REMOVE="yum remove -y"
        CMD_UPGRADE="yum update -y"
    fi
	if [[ $PMT == "apt"   ]];then
	        colorEcho $RED "不受支持的 Linux 系统"
            exit 1
	fi
    res=`which systemctl 2>/dev/null`
    if [[ "$?" != "0" ]]; then
        colorEcho $RED "系统版本过低,请升级到最新版本"
        exit 1
    fi
}


preinstall() {
	hostnamectl set-hostname $ROLE-$HOSTNAME
    #$PMT clean all
    [[ "$PMT" = "apt" ]] && $PMT update
    #echo $CMD_UPGRADE | bash
    echo ""colorEcho $BLUE" 安装必要软件 "if [["$PMT"="yum" ]]; then
        $CMD_INSTALL epel-release
    fi
    $CMD_INSTALL curl vim docker
	res=`which curl 2>/dev/null`
    [[ "$?" != "0" ]] && $CMD_INSTALL curl
	res=`which docker 2>/dev/null`
    [[ "$?" != "0" ]] && $CMD_INSTALL docker
	systemctl enable --now docker

	colorEcho $BLUE "配置服务器环境"
    if [[ -s /etc/selinux/config ]] && grep 'SELINUX=enforcing' /etc/selinux/config; then
        sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config
        setenforce 0
    fi
	echo 1 > /proc/sys/net/ipv4/ip_forward
	echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
	echo 'net.ipv4.ip_forward = 1' >>  /etc/sysctl.conf
	sysctl -p
	swapoff -a
}

installk8s(){
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
	colorEcho $BLUE '安装 k8s 组件'
	$CMD_INSTALL --nogpgcheck kubelet-1.23.5-0 kubeadm-1.23.5-0 kubectl-1.23.5-0
	systemctl enable --now kubelet

}

getip(){
	colorEcho $BLUE "请选择获取本机 ip/vip 的方式:"
    echo "1) 网络获取"
    echo "2) 手动输入"
    read -p "请选择获取本机 ip/vip 的方式 (默认联网获取):" answer
	if [[ -z "$answer" ]]; then
        METHOD="network"
    else
        case $answer in
        1)
            METHOD="network"
            ;;
		2)
            METHOD="matual"
            ;;
        *)
            colorEcho $RED "无效的选择,使用默认角色"
            METHOD="network"
        esac
    fi
    echo ""colorEcho $BLUE" 获取本机 ip/vip 的方式:$METHOD"if [[ $METHOD =='network' ]];then
		ip=`curl -s icanhazip.com`
	else
		read -p "请输入本机 ip/vip:" ip
	fi
	colorEcho $YELLOW "本机 ip/vip 地址为: $ip"
	read -p '确认安装请按回车键 (CTRL+ C 退出脚本)' a
}


init(){
cat <<-EOF >/root/kubeadm_init.yaml
apiVersion: kubeadm.k8s.io/v1beta3
kind: ClusterConfiguration
kubernetesVersion: v1.23.5
apiServer:
  certSANs:
  - "$ip"
controlPlaneEndpoint: "$ip:6443"
networking:
  podSubnet: 10.244.0.0/16
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
EOF
	kubeadm init --config=/root/kubeadm_init.yaml|tee /root/kubeinit.log
	mkdir -p $HOME/.kube
	cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
	chown $(id -u):$(id -g) $HOME/.kube/config
	kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
	colorEcho $GREEN 'k8s 安装完成'
}

greplog(){
        colorEcho $RED "关键命令,请注意保存"
        colorEcho $BLUE "如需部署集群"
        colorEcho $BLUE "请在其他 master 节点中执行下面命令"
        echo ` grep -E  -B 2 '\\-\\-control\-plane' /root/kubeinit.log|sed 's/\\\\//g'`
        colorEcho $BLUE "请在其他 node 节点执行完脚本后执行下面命令"
        echo `grep -E  -B 2 '\\-\\-control\-plane' /root/kubeinit.log |grep -v control|sed 's/\\\\//g'`
}


menu(){
	checkSystem
	colorEcho $BLUE "请选择本机的角色:"
    echo "1)master"
    echo "2)node"
    read -p "请选择 k8s 角色(默认 master)" answer
	if [[ -z "$answer" ]]; then
        ROLE="master"
    else
        case $answer in
        1)
            ROLE="master"
            ;;
		2)
            ROLE="node"
            ;;
        *)
            colorEcho $RED "无效的选择,使用默认角色"
            ROLE="master"
        esac
    fi
    echo ""colorEcho $BLUE" 当前 k8s 角色:$ROLE"if [[ $ROLE =='master' ]];then
		colorEcho $BLUE "检测到您设置的角色为 master,请选择行为:"
		echo "1) 建立新集群"
		echo "2) 加入旧集群"
		read -p "请选择 master 节点行为(默认建立新集群)" answer
		if [[ -z "$answer" ]]; then
			ACTION="new"
		else
			case $answer in
			1)
				ACTION="new"
				;;
			2)
				ACTION="old"
				;;
			*)
				colorEcho $RED "无效的选择,使用默认行为"
				ACTION="new"
			esac
		fi
		echo ""colorEcho $BLUE" 当前 k8s 节点行为:$ACTION"if [[ $ACTION =="new" ]];then
			getip
			preinstall
			installk8s
			init
			greplog
		else
			preinstall
			installk8s
			colorEcho $BLUE "master 节点安装完成,请在主 master 节点日志中拿取命令加入主节点"
			
		fi

	else
		preinstall
		installk8s
		colorEcho $BLUE "node 节点安装完成,请在主 master 节点日志中拿取命令加入主节点"
	fi
}

main(){menu}
main
正文完
 1
评论(没有评论)
验证码